# Built-In Example AI

# Title: DirectTurtle
# Author: Adam Rumpf
# Version: 1.0.1
# Date: 11/13/2020

import game.tcturtle
import math
import random
import datetime

class CombatTurtle(game.tcturtle.TurtleParent):
    """Direct combat turtle.

    Its main strategy is to try to move directly towards the opponent, firing
    missiles when it has clear line of sight. It does not pay much attention
    to obstacles.
    """
    is_turn = 0
    no_shoot_start = datetime.datetime.now()
    no_shoot_end = datetime.datetime.now()


    # -------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.
        """

        return "a_cfpolang"

    # -------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.
        """

        return "A smart turtles."

    # -------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.

        The return value can be either an integer or a tuple of tuples.

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.
        """

        return 1

    # =========================================================================

    def setup(self):
        self.spd = 1.0
        self.turn_spd = 0.5
        self.pursuit_range = 0.75 * self.missile_range

# -------------------------------------------------------------------------
#     控制角度朝向
    def heading_by_angle(self, angle=0):
        # print("x = ", self.x , "y = ", self.y)
        target = self.other_position

        if type(target) == int or type(target) == float:
            target = (
            int(self.x + 10000 * math.cos(math.radians(target))), int(self.y - 10000 * math.sin(math.radians(target))))

        if type(target) == tuple:
            turn = self.relative_heading_towards(target)
            turn = turn + angle
            if turn > 180:
                turn = turn - 360
            if turn < -180:
                turn = turn + 360
            if self.x == 0 or self.x == 800 or self.y == 0 or self.y == 800:  # 如果前面有障碍则转向
                print("x = ", self.x, "y = ", self.y)
                turn = turn - 360
                print("turn", turn)
            turn = min(max(turn, -self.max_turn_speed), self.max_turn_speed)
            turn /= self.max_turn_speed
            self.left(turn)


# -------------------------------------------------------------------------

    def step(self):
        health = self.health

        # 如果战斗力大于60，躲避攻击
        if health > 40:
            dir = self.distance()
            dir_keep = 150

            # datetime.datetime.now()
            if not self._other.can_shoot:
                self.no_shoot_start = datetime.datetime.now()
                self.no_shoot_end = datetime.datetime.now()
            else:
                self.no_shoot_end = datetime.datetime.now()

            between_shoot_time = (self.no_shoot_end - self.no_shoot_start).seconds

            # 危险状态躲避子弹
            if self._other.cooldown > 35 or (self._other.cooldown < 25 and dir < dir_keep and between_shoot_time < 10):
                # print("self.is_turn = ",self.is_turn)
                if self.is_turn == 0:
                    self.is_turn = random.randint(1, 4)
                if self.is_turn == 1:
                    self.heading_by_angle(90)
                    self.forward()
                elif self.is_turn == 2:
                    self.heading_by_angle(-90)
                    self.forward()
                elif self.is_turn == 3:
                    self.heading_by_angle(90)
                    self.backward()
                else:
                    self.heading_by_angle(-90)
                    self.backward()
            # 安全时朝向敌方
            else:
                if dir > 150:
                    self.heading_by_angle(random.randint(-90, 90))
                    self.forward()
                else:
                    self.is_turn = 0
                    self.heading_by_angle(0)
                    if dir > dir_keep:
                        self.forward()
                    else:
                        self.backward()
            if self.can_shoot and abs(self.relative_heading_towards()) <= 5 \
                    and self.line_of_sight() and self.distance() <= self.missile_range * 1.5:
                self.shoot()


        # 否则进行转右圈攻击模
        else:
            self.forward(self.spd)
            self.right(0.3)
            if self.can_shoot and abs(self.relative_heading_towards()) <= 10 \
                    and self.line_of_sight():
                self.shoot()




